﻿using spriteeditor;
using spriteeditor.Renderer;
using ttbit.WorkingSpace;

namespace pixeleditor.Editor
{
    public class Editor_Texture : IWorkResourceEditor
    {
        public string TypeName => Resource_Texture.TypeStr;

        Resource_Texture texture;

        int scale = 1; //保存scale
        Label uiScaleLabel;
        TrackBar uiTrackBar;
        public void OnActive(EditorApp app, IWorkResource resource)
        {
            texture = resource as Resource_Texture;
            if (texture == null)
            {
                return;
            }
            //设置Instpect功能
            app.EditorAreaInspector.Controls.Add(new Label() { Text = "Width=" + texture.Width, AutoSize = true });
            app.EditorAreaInspector.Controls.Add(new Label() { Text = "Height=" + texture.Height, AutoSize = true });


            //设置编辑功能
            {
                var line = new FlowLayoutPanel() { FlowDirection = FlowDirection.LeftToRight, AutoSize = true };
                app.EditorAreaFunction.Controls.Add(line);


                //app.EditorAreaInspector;

                uiScaleLabel = new Label() { Text = "Scale", Width = 128, Height = 24 };
                uiTrackBar = new TrackBar() { AutoSize = true, Maximum = 16, Minimum = 1, Value = scale };
                uiTrackBar.ValueChanged += (s, e) =>
                {
                    scale = uiTrackBar.Value;
                    uiScaleLabel.Text = "Scale X" + scale;
                };
                uiScaleLabel.Text = "Scale X" + scale;

                line.Controls.Add(uiScaleLabel);
                line.Controls.Add(uiTrackBar);

            }
            {
                var line = new FlowLayoutPanel() { FlowDirection = FlowDirection.LeftToRight, AutoSize = true };
                app.EditorAreaFunction.Controls.Add(line);
                var btn = new Button() { Text = "放回左上角" };
                line.Controls.Add(btn);
                btn.Click += (s, e) =>
                {
                    this.posx = 16;
                    this.posy = 16;
                };

            }

            posx = 16;
            posy = 16;
            pointdown = false;
        }
        public void OnDeactive(EditorApp app)
        {
            this.uiScaleLabel = null;
            this.texture = null;
            this.uiTrackBar = null;
        }
        int posx = 16;
        int posy = 16;
        public void OnRender(TTRender2 graphics)
        {
            if (texture != null && texture.texture != null)
            {

                int x2 = posx + texture.Width * scale;
                int y2 = posy + texture.Height * scale;
                graphics.DrawTexture(posx, posy, x2, y2, texture.texture, new Rectangle(0, 0, texture.Width, texture.Height));
                graphics.DrawBorder(posx, posy, x2, y2, 1, ttbit.math.ColorF.FromARGB(1, 1, 1, 0));
            }
        }
        int dx = 0;
        int dy = 0;
        bool pointdown;
        public void OnPointDown(int id, int x, int y)
        {
            pointdown = true;
            dx = x;
            dy = y;
        }
        public void OnPointMove(int id, int x, int y)
        {
            if (pointdown)
            {
                var mx = x - dx;
                var my = y - dy;
                this.posx += mx;
                this.posy += my;
                dx = x;
                dy = y;
            }
        }
        public void OnPointUp(int id, int x, int y)
        {
            pointdown = false;
        }
        public void OnPropChange()
        {

        }
    }
}
